package com.falcon.service.impl;

import java.util.List;

import com.falcon.mapper.IColumnMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.bean.BeanUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swan.mybatis.condition.Condition;
import com.falcon.mvc.domain.BasePager;
import com.falcon.core.utils.BeanUtils;

import com.falcon.domain.po.ColumnPO;
import com.falcon.domain.dos.ColumnDO;
import com.falcon.domain.req.ColumnQuery;
import com.falcon.domain.req.ColumnRequest;

import com.falcon.service.IColumnService;


@Slf4j
@Service
public class ColumnService implements IColumnService {

    @Autowired
    private IColumnMapper columnMapper;

    @Override
    public Long save(ColumnRequest request){
        ColumnPO columnPO = BeanUtil.copyProperties(request, ColumnPO.class);

        this.columnMapper.insertNotNull(columnPO);

        return columnPO.getId();
    }

    @Override
    public Boolean update(Long id, ColumnRequest request){

        ColumnPO columnPO = BeanUtil.copyProperties(request, ColumnPO.class);
        columnPO.setId(id);

        return this.columnMapper.updateNotNull(columnPO);
    }

    @Override
    public Boolean deleteById(Long id) {

        return this.columnMapper.deleteById(id);
    }

    @Override
    public ColumnDO queryById(Long id){

        ColumnPO columnPO = this.columnMapper.selectById(id);

        ColumnDO columnDO = BeanUtil.copyProperties(columnPO, ColumnDO.class);

        return columnDO;
    }


    @Override
    public List<ColumnDO> queryList(ColumnQuery request){

        Condition condition = request.toCondition();

        List<ColumnPO> poList = this.columnMapper.selectList(condition);

        List<ColumnDO> doList = BeanUtil.copyToList(poList, ColumnDO.class);

        return doList;
    }

    @Override
    public BasePager<ColumnDO> queryPager(ColumnQuery request){


        PageInfo<ColumnPO> pageInfo = PageHelper.startPage(request.getPage(), request.getPageSize())
                .doSelectPageInfo(() -> this.columnMapper.selectList(request.toCondition()));

        return BeanUtils.toPager(pageInfo, ColumnDO.class);
    }

    @Override
    public int batchDelete(List<Long> ids) {

        return this.columnMapper.deleteByIds(ids);
    }


}
